package com.lovo.redis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import com.lovo.redis.bean.TianMaoBean;
import com.lovo.redis.config.RedisCacheUtil;
import com.lovo.redis.service.ITianMaoService;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class CacheController {
    @Autowired
    RedisCacheUtil redisCacheUtil;
    @Autowired
    ITianMaoService service;

    @Autowired
    RLock rLock;
//    public static BloomFilter bloomFilter= BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()),1000000,0.001);
//    static {
//        bloomFilter.put("万达_20");
//        bloomFilter.put("万达_30");
//    }

    @GetMapping("ticketInfo/{cinemaName}/{tickePrice}")
    public Object ticketInfo(@PathVariable("cinemaName")String cinemaName,
                             @PathVariable("tickePrice") Integer tickePrice) throws InterruptedException {

        return  this.cache(cinemaName,tickePrice);
    }

    public Object  cache(String cinemaName,float tickePrice) throws InterruptedException {
        String key=cinemaName+"_"+tickePrice;

        //防止缓存穿透
//        if(bloomFilter.mightContain(key)){

            if(!redisCacheUtil.hasKey(key)){
                boolean bl=    rLock.tryLock(0,10, TimeUnit.SECONDS);
                if(bl){
                    //去数据库查询
                    QueryWrapper<TianMaoBean> qw=new QueryWrapper();
                    qw.eq("sm_name",cinemaName);
                    qw.gt("sm_price",tickePrice);
                    List<TianMaoBean> list=   service.list(qw);
                    int cacheTime= (int) ((System.currentTimeMillis()+key.hashCode())%RedisCacheUtil.MAX_TIME);
                    if(cacheTime<RedisCacheUtil.MIN_TIME){
                        cacheTime=RedisCacheUtil.MIN_TIME;
                        System.out.println(cacheTime+"1");
                    }
                    if(null!=list) {
                        redisCacheUtil.set(key, list, cacheTime);
                        System.out.println(cacheTime+"2");
                    }else {
                        redisCacheUtil.set(key, list, RedisCacheUtil.NULL_TIME);
                        System.out.println(cacheTime+"3");
                    }
                    rLock.unlock();//释放锁
                    System.out.println(1);
                    return  list;
                }else {

                    Thread.sleep(500);
                    System.out.println(2);
                    this.cache(cinemaName,tickePrice);
                }

            }else {
                System.out.println(3);
                //缓存存在
                return redisCacheUtil.get(key);
            }
//        }
        return null;

    }


    @GetMapping("ticketInfo2/{cinemaName}/{tickePrice}")
    public Object ticketInfo2(@PathVariable("cinemaName")String cinemaName,
                              @PathVariable("tickePrice")float tickePrice){
        QueryWrapper<TianMaoBean> qw=new QueryWrapper();
        qw.eq("sm_movieName",cinemaName);
        qw.eq("sm_price",tickePrice);
        List<TianMaoBean> list=   service.list(qw);
        return list;
    }
}
